package cn.loioi.web.components.beetl.fun;

import org.beetl.core.Context;
import org.beetl.core.Function;

import com.github.pagehelper.PageInfo;

/**
 * <br>
 * (c) Copyright 2015 by <br>
 * 
 * @description : 分页按键 <br>
 * @file_name :PageUtil.java <br>
 * @author :yyzh <br>
 * @创建时间 :2015年12月16日 下午7:01:05
 */
public class PageUtil implements Function {
	private static final int maxPages = 2;

	/**
	 * PageInfo 对象 Url 跳转地址 class 分页插件class
	 */
	@Override
	public Object call(Object[] paras, Context ctx) {
		StringBuffer stringBuffer = new StringBuffer();
		if (paras[0] instanceof PageInfo<?>) {
			PageInfo<?> pageInfo = (PageInfo<?>) paras[0];
			Integer pageNo = pageInfo.getPageNum();
			Integer pageSize = pageInfo.getPageSize();
			Integer pages = pageInfo.getPages();
			String url = null;
			String clzss = null;
			if (paras.length >= 3) {
				url = (String) paras[1];
				clzss = (String) paras[2];
			} else if (paras.length >= 2) {
				url = (String) paras[1];
			}
			url = url(url, pageInfo.getPageSize());
			stringBuffer.append("<ul class='pagination " + clzss + "'>");
			stringBuffer.append(prev(url, pageNo, pageSize));
			for (int i = 1; i <= pages; i++) {
				if (pageNo == i) {
					stringBuffer.append(liActive(url, i));
				} else if (pageNo > (maxPages + 1) && i == (maxPages + 1)) {
					stringBuffer.append(liOmitted(url, i));
				} else if (pageNo < (pages - maxPages) && i == (pages - maxPages)) {
					stringBuffer.append(liOmitted(url, i));
				} else if (i <= maxPages || i > (pages - maxPages)) {
					stringBuffer.append(li(url, i));
				} else {
					continue;
				}
			}
			stringBuffer.append(next(url, pageNo, pages));
			stringBuffer.append("</ul>");
		} else {
			stringBuffer.append("<ul class='pagination pagination-sm'><li><span>1</span></li></ul>");
		}
		return stringBuffer.toString();
	}

	/**
	 * 上一页
	 * 
	 * @param url
	 * @param pageNo
	 * @param pages
	 * @return
	 */
	public static String prev(String url, int pageNo, int pages) {
		if (pageNo == 1) {
			return ("<li><span>&laquo;</span></li>\n");
		} else {
			return ("<li><a href='" + url + (pageNo - 1) + "'>&laquo;</a></li>\n");
		}
	}

	/**
	 * 下一页
	 * 
	 * @param url
	 * @param pageNo
	 * @param pages
	 * @return
	 */
	public static String next(String url, int pageNo, int pages) {
		if (pageNo == pages) {
			return ("<li><span>&raquo;</span></li>\n");
		} else {
			return ("<li><a href='" + url + (pageNo + 1) + "'>&raquo;</a></li>\n");
		}
	}

	/**
	 * 页码链接
	 * 
	 * @param url
	 * @param pageNo
	 * @return
	 */
	public static String li(String url, int pageNo) {
		return ("<li><a href='" + url + pageNo + "'>" + pageNo + "</a></li>\n");
	}

	/**
	 * 当前页码
	 * 
	 * @param url
	 * @param pageNo
	 * @return
	 */
	public static String liActive(String url, int pageNo) {
		return ("<li class='active'><span>" + pageNo + "</span></li>\n");
	}

	/**
	 * 省略号
	 * 
	 * @param url
	 * @param pageNo
	 * @return
	 */
	public static String liOmitted(String url, int pageNo) {
		return ("<li><span>...</span></li>\n");
	}

	/**
	 * 生成url地址
	 * 
	 * @param url
	 * @param pageSize
	 * @return
	 */
	public static String url(String url, int pageSize) {
		if (url.indexOf("?") == -1) {
			return (url + "?pageSize=" + pageSize + "&pageNo=");
		} else {
			if (url.matches("(\\S*)pageSize=\\d+(\\S*)")) {
				url = url.replaceAll("pageSize=\\d+", "pageSize=" + pageSize);
			} else {
				url += ("&pageSize=" + pageSize);
			}
			if (url.matches("(\\S*)pageNo=\\d+(\\S*)")) {
				url = url.replaceAll("pageNo=\\d+", "pageNo=");
			} else {
				url += ("&pageNo=");
			}
		}
		return url;
	}

}
